#定义工具链
TOOLCHAIN = arm-none-eabi-
#定义编译器
CC        = $(TOOLCHAIN)gcc
AS        = $(TOOLCHAIN)as
LD        = $(TOOLCHAIN)ld
OBJCOPY   = $(TOOLCHAIN)objcopy
OBJDUMP   = $(TOOLCHAIN)objdump
#定义编译标志
CCFLAGS   = -Wall -mcpu=cortex-m3 -mthumb -nostartfiles -nostdinc -nostdlib -T boot.ld
ASFLAGS   = -mcpu=cortex-m3 -mthumb
LDFLAGS   = -T boot.ld
CPFLAGS   = -Obinary
DUFLAGS   = -S -D
DEBUG	  = -g
#导出全局变量
export CC AS LD OBJCOPY OBJDUMP
export CCFLAGS ASFLAGS LDFLAGS CPFLAGS DUFLAGS DEBUG
#目标文件路径
BOOT_PATH = .\boot
DEVICE_PATH = .\device
#生成的目标
TARGET    = boot.bin
#需要连接的目标文件
OBJS = $(shell for /r boot %%i in (*.o) do @echo %%i) $(shell for /r device %%i in (*.o) do @echo %%i) 
#目标生成规则
all:BOOT_OBJS DEVICE_OBJS $(TARGET)
#生成反汇编代码
	$(OBJDUMP) $(DUFLAGS) boot.out > boot.list
#进入各子目录编译
BOOT_OBJS:
	make -C $(BOOT_PATH)
DEVICE_OBJS:
	make -C $(DEVICE_PATH)
	
$(TARGET):boot.out
#生成二进制代码，格式为bin
	$(OBJCOPY) $(CPFLAGS) boot.out $(TARGET)
	
boot.out:
#链接目标代码
	$(LD) $(LDFLAGS) -o boot.out $(OBJS)

#伪指令，用于清除文件
.PHONY:clean
clean:
	make -C $(BOOT_PATH) clean
	make -C $(DEVICE_PATH) clean
	del *.bin *.out *.list